#!/bin/bash
# filepath: /Users/aibokalv/Mycode/coding_pc_nuxtcoding_pc_nuxt/scripts/test-docker.sh

set -e

echo "🐳 开始 Docker 构建测试..."

# 构建 Docker 镜像
echo "🔨 构建 Docker 镜像..."
docker build -t typing-pc-nuxt:test .

# 检查镜像大小
echo "📊 镜像大小："
docker images typing-pc-nuxt:test

# 运行容器
echo "🚀 启动容器..."
docker run -d --name typing-test -p 3001:3000 typing-pc-nuxt:test


# 等待容器启动
echo "⏳ 等待容器启动..."
sleep 10

# 测试健康检查
echo "🩺 测试健康检查..."
curl -f http://localhost:3001/api/health || {
    echo "❌ 容器健康检查失败！"
    docker logs typing-test
    docker stop typing-test
    docker rm typing-test
    exit 1
}

echo "✅ 容器健康检查通过！"

# 测试基本页面
echo "🌐 测试基本页面..."
curl -f http://localhost:3001/ > /dev/null || {
    echo "❌ 页面访问失败！"
    docker logs typing-test
    docker stop typing-test
    docker rm typing-test
    exit 1
}

echo "✅ 页面访问正常！"

# 清理
echo "🧹 清理测试容器..."
docker stop typing-test
docker rm typing-test

echo "🎉 Docker 构建测试完成！"